<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建记录
                    </span>
                    <!--返回列表-->
                    <span style="margin-left: 530px;" @click="tzdz">
                        <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                    </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 50px;" size="mini" :rules="rules" :model="jilulist" ref="jilulist">
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">主题：</label><br/>
                                    <el-input v-model="jilulist.returnedmoneylogTheme"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;" prop="clientId">
                                    <label slot="label" style="font-size: 13px">客户：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="kehu">
                                        <span style="font-size: 16px;">关联客户模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="kehulist"
                                                :data="kehulist">
                                            <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                            <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                            <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                            <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                            <el-table-column property="clientSite" label="地址"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize"
                                                :current-page.sync="currPage"
                                                @current-change="handleCurrentChange"
                                                :total="totalSize">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzkehu">保 存</el-button>
                                            <el-button type="primary" @click="kehu = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="jilulist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1||this.xg==2" :disabled="true" v-model="jilulist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px" prop="returnedmoneyId">
                                    <label slot="label" style="font-size: 13px">回款/退款计划：</label><br/>
                                    <!--回款计划-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="hueikuan">
                                        <span style="font-size: 16px;">关联计划模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2" v-model="hkinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 100px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="hueikuanlist"
                                                :data="hueikuanlist">
                                            <el-table-column type="selection" width="50" align="center" property="returnedmoneyId"></el-table-column>
                                            <el-table-column width="150" property="returnedmoneyTheme" label="主题"></el-table-column>
                                            <el-table-column label="计划回款日期" align="center" width="200">
                                                <template slot-scope="d">
                                                    {{d.row.returnedmoneyTime | formatDate}}
                                                </template>
                                            </el-table-column>
                                            <el-table-column width="100" property="returnedmoneyIssue" label="期次"></el-table-column>
                                            <el-table-column width="100" property="returnedmoneyMoney" label="金额"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="total, prev, pager, next"
                                                @current-change="handleCurrentChange2"
                                                :page-size="pageSize2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzhueikuan">保 存</el-button>
                                            <el-button type="primary" @click="hueikuan = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="jilulist.returnedmoneyId.returnedmoneyTheme" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1 ||this.xg==2" :disabled="true" v-model="jilulist.returnedmoneyId.returnedmoneyTheme" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;" prop="returnedmoneylogMoney">
                                    <label slot="label" style="font-size: 13px">金额：</label><br/>
                                    <el-input v-model="jilulist.returnedmoneylogMoney" :disabled="true" style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item prop="returnedmoneylogTime">
                                    <label slot="label" style="font-size: 13px">付款日期：</label><br/>
                                    <el-date-picker
                                            v-if="this.xg==0 ||this.xg==2"
                                            v-model="jilulist.returnedmoneylogTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                    <el-date-picker
                                            v-if="this.xg==1"
                                            :disabled="true"
                                            v-model="jilulist.returnedmoneylogTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">期次：</label><br/>
                                    <el-input  v-model="jilulist.returnedmoneylogIssue" :disabled="true" style="width: 250px;"></el-input>
                                    </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">外币备注：</label><br/>
                                    <el-input :disabled="true" v-model="jilulist.returnedmoneylogBuck"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="returnedmoneylogRate">
                                    <label slot="label" style="font-size: 13px">与人民币汇率：</label><br/>
                                    <el-input :disabled="true" v-model="jilulist.returnedmoneylogRate"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">付款方式：</label><br/>
                                    <el-input :disabled="true" v-model="jilulist.returnedmoneylogPayment"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input  :disabled="true" v-model="jilulist.returnedmoneylogOwner"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item>
                                    <label slot="label" style="font-size: 13px">备注：</label><br/>
                                    <textarea style="width: 580px;" v-model="jilulist.returnedmoneylogRemark" placeholder="备注说明"></textarea>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <br/>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">
                    <el-button  @click="tzdz">取 消</el-button>
                    &nbsp;&nbsp;
                    <el-button type="primary" @click="addbusiness('jilulist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "ZxinzenHueikuanJihua",
        data () {
            var hueikuan = (rule, value, callback) => {
                if (this.jilulist.clientId.clientName === null || this.jilulist.clientId.clientName === '') {
                    callback(new Error('请先选择客户'));
                } else {
                    if (this.jilulist.returnedmoneyId.returnedmoneyTheme === '' || this.jilulist.returnedmoneyId.returnedmoneyTheme === null) {
                        callback(new Error('请选择计划'));
                    }else {
                        callback()
                    }
                }
            };
            var kehua = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            var jine = (rule, value, callback) =>   {
                if (value === '') {
                    callback(new Error('请输入金额'));
                } else {
                    var numReg = /^[0-9]*$/
                    var numRe = new RegExp(numReg)
                    if (!numRe.test(value)) {
                        callback(new Error('金额只能为数字'));
                    }else {
                        if (value <=0) {
                            callback(new Error('金额不能小于0'));
                        }else {
                            callback()
                        }
                    }
                }
            };
            var hueilv = (rule, value, callback) => {
                if (!value) {
                    callback()
                }else{
                    if(!this.jilulist.returnedmoneylogBuck){
                        callback(new Error('请先输入外币备注'));
                        this.jihualist.returnedmoneylogRate='';
                    } else {
                        callback()
                    }
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehua, required: true, trigger: ['blur','change']},
                    ],
                    returnedmoneylogOwner: [
                        { required: true, message: '请选择负责人' , trigger: 'change'},
                    ],
                    returnedmoneyId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    returnedmoneylogTime: [
                        {required: true, message: '请选择回款时间', trigger: 'change' },
                    ],
                    returnedmoneylogMoney: [
                        {validator: jine, required: true, trigger: ['blur','change']},
                    ],
                    returnedmoneylogRate: [
                        {validator: hueilv, trigger: 'blur'},
                    ]
                },
                pickerOptions: {//开票日期快捷选择方式
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                kehu:false,//客户弹出框
                hueikuan:false,//回款弹出框
                kehuinput:'',//客户弹框搜索框
                hkinput:'',//回款弹框搜索框
                hueikuanlist:[],//回款表格信息
                kehulist:[],//客户表格信息
                jilulist:{
                    returnedmoneylogId:0,
                    returnedmoneyId:{
                        returnedmoneyId:0,
                        returnedmoneyTheme:''
                    },
                    clientId:{
                        clientId:0,
                        clientName:''
                    },
                    returnedmoneylogTheme:'',
                    returnedmoneylogMoney:0,
                    returnedmoneylogTime:'',
                    returnedmoneylogIssue:'',
                    returnedmoneylogOwner:'',
                    returnedmoneylogPayment:'',
                    returnedmoneylogAccessory:null,
                    returnedmoneylogRefund:'',
                    returnedmoneylogBuck:null,
                    returnedmoneylogRate:null,
                    returnedmoneylogRemark:null,
                    returnedmoneylogZt:0,
                    returnedmoneylogInvoice:'未开发票'
                    },//表单
                pageSize:4,//客户分页页大小
                currPage:1,//客户当前页码，默认为1
                totalSize:0,//客户总记录数
                pageSize2:4,//回款分页页大小
                currPage2:1,//回款当前页码，默认为1
                totalSize2:0,//回款总记录数
                hkkehu:0,//回款页面绑定客户查询
                xg:0,//判断是新增还是修改

                ys:1,
                ydx:10,
                dz:'',
                bq:''
            };
        },
        methods: {
            tzdz(){
                if(this.dz==='hkzy'){
                    this.$router.push({
                        path:'/huikuan',
                        query: {ys:this.ys,ydx:this.ydx,row:this.bq}
                    });
                }else if(this.dz==='hkjlzy'){
                    this.$router.push({
                        path:'/huikuanjilu',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            /*当前页方法*/
            handleCurrentChange(val) {
                this.initData(val);
            },
            /*当前页方法*/
            handleCurrentChange2(val) {
                this.hkinitData(val);
            },
            /*客户查询方法*/
            cx() {
                this.initData(this.currPage);
            },
            /*计划查询方法*/
            cx2() {
                this.hkinitData(this.currPage2);
            },
            //确定按钮
            addbusiness(formName) {
                window.console.log(this.jilulist.returnedmoneylogRate)
                if(this.xg!=0){
                    this.xzfapiao();
                }else{
                    this.$refs[formName].validate(valid => {
                        this.print(valid);
                        window.console.log(valid)
                        if (valid) {
                            this.xzfapiao();
                        }else{
                            this.$message({
                                message: '验证不通过！',
                                type: 'warning'
                            });
                            return false;
                        }

                    });
                }
            },
            //点击回款计划输入框就查询回款计划
            dakai(){
                window.console.log("aaa")
                this.hkinitData(this.currPage2);
            },
            //新增记录
            xzfapiao(){
                this.$axios.post("http://localhost:8088/hkjl/add_jh",this.jilulist)
                    .then(()=> {
                        if (this.xg == 1) {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                            this.$router.push({
                                path: '/zhuye/hueikuanjilu'
                            });
                        } else {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });
                            this.$router.push({
                                path: '/zhuye/hueikuanjilu'
                            });
                        }
                    }).catch(()=>{
                        if(this.xg==1){
                            this.$message({
                                showClose: true,
                                message: '修改失败',
                                type: 'error'
                            });
                        }else{
                            this.$message({
                                showClose: true,
                                message: '新增失败',
                                type: 'error'
                            });
                        }
                    }
                )
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro)=>{
                    this.jilulist.clientId=pro;
                });
                this.kehu=false;
                this.kehuinput='';
            },
            //选择回款计划确定按钮
            xzhueikuan() {
                this.$refs.hueikuanlist.selection.forEach((pro)=>{
                    this.jilulist.returnedmoneyId=pro;
                    this.jilulist.returnedmoneylogRefund=pro.returnedmoneyRefund;
                    this.jilulist.returnedmoneylogMoney=pro.returnedmoneyMoney;
                    this.jilulist.returnedmoneylogIssue=pro.returnedmoneyIssue;
                    this.jilulist.returnedmoneylogBuck=pro.returnedmoneyBuck;
                    this.jilulist.returnedmoneylogRate=pro.returnedmoneyRate;
                    this.jilulist.returnedmoneylogPayment=pro.returnedmoneyPayment;
                });
                this.hueikuan=false;
                this.hkinput='';
            },
            //客户查询方法
            initData(aa){
                this.$axios.post("http://localhost:8088/dindan/kehu_list", this.qs.stringify(
                    {search:this.kehuinput, page:aa, size:this.pageSize,}))
                    .then(v=>{
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            //回款计划查询方法
            hkinitData(aa){
                this.hkkehu=this.jilulist.clientId.clientId;
                window.console.log(this.hkinput)
                this.$axios.get("http://localhost:8088/hkjh_list",
                    {params:{page:aa,size:this.pageSize2,search:this.hkinput,search2: this.hkkehu}})
                    .then(v=>{
                            this.hueikuanlist = v.data.rows;
                            this.totalSize2 = v.data.total;
                        }
                    ).catch()
            },
        },
        created: function () {
            this.jilulist.returnedmoneylogOwner= JSON.parse(localStorage.getItem("user")).empName;
            if(this.$route.query.key!=null){
                this.jilulist=this.$route.query.key;
                this.xg = 1;
            }
            if(this.$route.query.hk!=null){
                this.jilulist.clientId=this.$route.query.hk.clientId;
                this.jilulist.returnedmoneyId=this.$route.query.hk;
                this.jilulist.returnedmoneylogRefund=this.$route.query.hk.returnedmoneyRefund;
                this.jilulist.returnedmoneylogMoney=this.$route.query.hk.returnedmoneyMoney;
                this.jilulist.returnedmoneylogIssue=this.$route.query.hk.returnedmoneyIssue;
                this.jilulist.returnedmoneylogBuck=this.$route.query.hk.returnedmoneyBuck;
                this.jilulist.returnedmoneylogRate=this.$route.query.hk.returnedmoneyRate;
                this.jilulist.returnedmoneylogPayment=this.$route.query.hk.returnedmoneyPayment;
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
            if(this.$route.query.bq!=null){
                this.bq=this.$route.query.bq;
            }
            this.initData(this.currPage);
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }
</style>